package messageHandler;

import java.util.Vector;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Animal;
import model.BoardInfo;
import model.IAction;
import model.PlayerInfo;
import model.RoomInfo;
import network.Cmd_Message;
import network.Message;
import screen.BauCuaScr;
import screen.BoardListScr;
import screen.FightingScr;
import screen.PlayGameScr;
import screen.QuestPetScr;
import screen.RoomListScr;
import screen.StudyEnglishScr;
import service.GlobalService;

public class GameCenterMsgHandler implements IMiniGameMsgHandler{
	public static PlayGameScr curScr;
	
	public void onMessage(Message msg) {
		// TODO Auto-generated method stub
		PlayerInfo p;
		try {
            switch (msg.command) {
            case Cmd_Message.ROOM_LIST:
            	Vector roomList = new Vector();
        		while (msg.reader().available() > 0) {
        			RoomInfo roomInfo = new RoomInfo();
        			roomInfo.m_iId = msg.reader().readByte();
        			roomInfo.roomFree = msg.reader().readByte();					
        			roomInfo.roomWait = msg.reader().readByte();					
        			roomInfo.lv = msg.reader().readByte();
        			roomList.addElement(roomInfo);
        		}
        		RoomListScr.gI().setListItem(roomList);
        		        		
        		GameCanvas.endDlg();
        		RoomListScr.gI().switchToMe();
        		
        		break;
        		
            case Cmd_Message.BOARD_LIST:
            	//relase old resource
            	RoomListScr.gI().clearData();
            	
            	Vector boardList = new Vector();
        		final byte roomID = msg.reader().readByte();		

        		while (msg.reader().available() > 0) {					
        			BoardInfo boardInfo = new BoardInfo();
        			
        			boardInfo.m_iId = msg.reader().readByte();
        			boardInfo.nPlayer = msg.reader().readByte();	
        			boardInfo.maxPlayer = msg.reader().readByte();	
        			boardInfo.isPlaying = msg.reader().readBoolean();
        			boardInfo.isPass = msg.reader().readBoolean();
        			boardInfo.money = msg.reader().readInt();		

        			boardList.addElement(boardInfo);
        		}
        		
        		BoardListScr.gI().setListItem(boardList);
        		BoardListScr.gI().m_iRoomID = roomID;
        		
        		GameCanvas.endDlg();
        		BoardListScr.gI().switchToMe();

            	break;
            	
            case Cmd_Message.JOIN_BOARD:
            	BoardListScr.gI().clearData();
            	
            	byte roomID1 = msg.reader().readByte();
        		byte boardID = msg.reader().readByte();
        		int ownerID = msg.reader().readInt();
        		int money = msg.reader().readInt();				
        		Vector vtPlayer = new Vector();
        		while (msg.reader().available() > 0) {					
        			PlayerInfo playerInfo = new PlayerInfo();
        			playerInfo.m_iId = msg.reader().readInt();
        			if (playerInfo.m_iId == -1) {
        				playerInfo.m_strName = "";
        				playerInfo.m_UserName = "";
        				
        			} else {
        				playerInfo.m_UserName = msg.reader().readUTF();
        				playerInfo.setShortName(playerInfo.m_UserName);
        				playerInfo.m_iMoney[0] = msg.reader().readInt();
        				playerInfo.setAvatar(msg.reader().readShort());
        				playerInfo.m_level = msg.reader().readInt();
        				playerInfo.isReady = msg.reader().readBoolean();
        				
        				//get default animal
        				short petType = msg.reader().readShort();
        				if(playerInfo.m_defaultPet != null)
        					playerInfo.m_defaultPet = null;
        				playerInfo.m_defaultPet = new Animal(petType);
        				playerInfo.m_defaultPet.m_iId = msg.reader().readShort();
        				playerInfo.m_defaultPet.m_iHealth = msg.reader().readShort();
        				playerInfo.m_defaultPet.m_iBonusHealth = msg.reader().readShort();
        				
        			}									
        			vtPlayer.addElement(playerInfo);
        		}	
        		GameCanvas.endDlg();

        		QuestPetScr.clean();
        		StudyEnglishScr.clean();
        		FightingScr.clean();
        		BauCuaScr.clean();
        		
        		switch(RoomListScr.m_iIconGame){
            	case 15://bau cua
            		GameQuestPetMsgHandler.curScr = BauCuaScr.gI();
            		break;
            	case 16://doan thu
            		GameQuestPetMsgHandler.curScr = QuestPetScr.gI();
            		break;
            	case 12://dua thu
            		break;
            	case 13://hoc tieng anh
            		GameQuestPetMsgHandler.curScr = StudyEnglishScr.gI();
            		break;
            	case 14://quay so
            		break;
            	case 11://song dau
            		GameQuestPetMsgHandler.curScr = FightingScr.gI();
            		break;
            	case 17://tim cap
            		break;
        		}
        		
        		curScr.switchToMe(roomID1, boardID, vtPlayer, ownerID, money);
        		break;
        		
        	case Cmd_Message.PLAYER_JOIN_BOARD:		
        		p = new PlayerInfo();
        		int seat = msg.reader().readByte();
        		p.m_iId = msg.reader().readInt();
        		p.m_UserName = msg.reader().readUTF();
        		p.setShortName(p.m_UserName);
        		p.m_iMoney[0] = msg.reader().readInt();	
        		p.setAvatar(msg.reader().readShort());
        		p.m_level = msg.reader().readInt();

				//get default animal
				short petType = msg.reader().readShort();
				if(p.m_defaultPet != null)
					p.m_defaultPet = null;				
				p.m_defaultPet = new Animal(petType);
				p.m_defaultPet.m_iId = msg.reader().readShort();
				p.m_defaultPet.m_iHealth = msg.reader().readShort();
				p.m_defaultPet.m_iBonusHealth = msg.reader().readShort();
				
        		p.isReady=false;	
        		curScr.setPlayer(p, seat);
        		break;	
        		
            case Cmd_Message.LEAVE_BOARD:
            	int IDLeave = msg.reader().readInt();
				int IDNewOwner = msg.reader().readInt();
				
				curScr.playerLeave(IDLeave);
				curScr.setOwner(IDNewOwner);
				break;
				
            case Cmd_Message.SET_MONEY:
            	roomID1 = msg.reader().readByte();
				boardID = msg.reader().readByte();
				int money3 = msg.reader().readInt();
				curScr.setMoney(money3);
            	break;
            	
            case Cmd_Message.KICK_PLAYER:
            	roomID1 = msg.reader().readByte();
            	boardID = msg.reader().readByte();
				int kicked = msg.reader().readInt();				
				//
				curScr.playerLeave(kicked);
				if (kicked == NuoiThuMidlet.m_myPlayerInfo.m_iId)
				{
					GameCanvas.startOK("Bạn đã bị kick bởi chủ sàn", new IAction(){
						public void perform(){
							GlobalService.gI().requestBoardList(curScr.m_iRoomID);
						}
					});
				}
            	break;
            	
            case Cmd_Message.READY:
            	int ID = msg.reader().readInt();
				boolean isReady = msg.reader().readBoolean();
				if (ID == NuoiThuMidlet.m_myPlayerInfo.m_iId)
					GameCanvas.endDlg();
				curScr.setReady(ID, isReady);
            	break;
            	
            case Cmd_Message.BONUS_MONEY:
            	roomID1 = msg.reader().readByte();
    			boardID = msg.reader().readByte();
    			int playerId = msg.reader().readInt();
    			int value = msg.reader().readInt();
    			String text = msg.reader().readUTF();
    			curScr.onBonusMoney(playerId, value, text);
    			break;
            case Cmd_Message.CHAT_BOARD:
            	roomID1 = msg.reader().readByte();
    			boardID = msg.reader().readByte();
    			playerId = msg.reader().readInt();
    			text = msg.reader().readUTF();
    			curScr.showChat(playerId, text);
            	
            	break;
            }
		}
		catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
}
